home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5701 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.0 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: String Arrays and string parsing
  5. Date: Tue, 20 Feb 96 20:01:40 GMT
  6. Organization: none
  7. Message-ID: <824846500snz@genesis.demon.co.uk>
  8. References: <31287278.789445@news.inforamp.net> <4gaqrj$3kn@hacgate2.hac.com>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4gaqrj$3kn@hacgate2.hac.com>
  15.            collins@thor.tu.hac.com "Ron Collins" writes:
  16.  
  17. >Danny Heuman (dsheuman@inforamp.net) wrote:
  18. >: I have day, month, and year as DDMMMYYYY and would like to parse it
  19. >: out in to DD, MMM, YYYY.  I can parse the DD out by using
  20. >: strncpy(into1, from1, 2).  I can parse the YYYY out by doing
  21. >: strcpy(into3, from1 + 5).  How can I parse out the MMM?  Can I use
  22. >: either of these functions that work above, strncpy or strcpy?  If so,
  23. >: once parsed, do I have to attach an '\0' to the end of it to keep it
  24. >: as a character string?
  25. >
  26. >: Thanks,
  27. >
  28. >
  29. >: Danny Heuman
  30. >: dsheuman@inforamp.net
  31. >
  32. >You've almost got it with the "strncpy()".  Try this:
  33. >(assuming you define "from1" somewhere)
  34. >
  35. >#include <string.h>
  36. >
  37. >....
  38. >
  39. >char  into1[3];
  40. >char  into2[4];
  41. >char  into3[5];
  42. >
  43. >...
  44. >
  45. >   strncpy(into1,from1,2);
  46. >   into1[2] = 0;
  47. >   strncpy(into2,from1+2,3);
  48. >   into2[3] = 0;
  49. >   strncpy(into3,from1+5,4);
  50. >   into3[4] = 0;
  51.  
  52. A possibly better approach in general (although it doesn't make much
  53. difference here) is:
  54.  
  55.    *into1 = '\0';
  56.    strncat(into1,from1,2);
  57.    *into2 = '\0';
  58.    strncat(into2,from1+2,3);
  59.    *into3 = '\0';
  60.    strncat(into3,from1+5,4);
  61.  
  62. Or use strcpy() for the last if you can guarantee that from1 is null
  63. terminated after YYYY. A more compact approach is
  64.  
  65.    sscanf(from1, "%2s%3s%4s", into1, into2, into3);
  66.  
  67. -- 
  68. -----------------------------------------
  69. Lawrence Kirby | fred@genesis.demon.co.uk
  70. Wilts, England | 70734.126@compuserve.com
  71. -----------------------------------------
  72.